package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbUserScoreAccountMapper;
import com.dongdongshop.pojo.TbUserScoreAccount;
import com.dongdongshop.pojo.TbUserScoreAccountExample;
import com.dongdongshop.service.TbUserScoreAccountService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@DubboService
public class TbUserScoreAccountServiceImpl implements TbUserScoreAccountService {

    //注入mapper
    @Autowired
    private TbUserScoreAccountMapper tbUserScoreAccountMapper;

    @Override
    public void add(Long userid, Long out_trade_no, Long receipt_amount) {
        TbUserScoreAccountExample te = new TbUserScoreAccountExample();
        TbUserScoreAccountExample.Criteria criteria = te.createCriteria();
        criteria.andUserIdEqualTo(userid);
        List<TbUserScoreAccount> tbUserScoreAccounts = tbUserScoreAccountMapper.selectByExample(te);
        if (tbUserScoreAccounts.size()==0){
            TbUserScoreAccount account = new TbUserScoreAccount();
            account.setUserId(userid);
            account.setNum(receipt_amount);
            account.setUpdateDate(new Date());
            account.setNewDate(new Date());
            tbUserScoreAccountMapper.insertSelective(account);
        }else {
            tbUserScoreAccounts.get(0).setUpdateDate(new Date());
            tbUserScoreAccounts.get(0).setNum(tbUserScoreAccounts.get(0).getNum()+receipt_amount);
            tbUserScoreAccountMapper.updateByPrimaryKeySelective(tbUserScoreAccounts.get(0));
        }
    }
}
